package com.cxlin.cwanandroid.ui.home.fragment

import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.QuickAdapterHelper
import com.chad.library.adapter4.loadState.LoadState
import com.cxlin.base.BaseFragment
import com.cxlin.base.ext.binding
import com.cxlin.base.ext.startActivity
import com.cxlin.base.ext.toast
import com.cxlin.base.mvi.ext.collectFLow
import com.cxlin.cwanandroid.R
import com.cxlin.cwanandroid.databinding.FragmentSystemBinding
import com.cxlin.cwanandroid.ui.home.adapter.SystemAdapter
import com.cxlin.cwanandroid.ui.home.viewmodel.SystemViewModel
import com.cxlin.cwanandroid.ui.login.viewmodel.LoginViewModel
import com.cxlin.cwanandroid.ui.systemtype.SystemTypeNormalActivity
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 *
 * @Author: chenxunlin
 * @Date: 2024/4/30 15:00
 */
class SystemFragment : BaseFragment() {

    private val binding by binding<FragmentSystemBinding>()
    private val viewModel by viewModel<SystemViewModel>()
    private lateinit var helper: QuickAdapterHelper
    private val systemAdapter by lazy {
        SystemAdapter()
    }

    override fun getLayoutId() = R.layout.fragment_system

    override fun initView() {
        initRecyclerView()
    }

    private fun initRecyclerView() {
        systemAdapter.run {
            addOnItemChildClickListener(R.id.root) { adapter, view, position ->
                requireActivity().startActivity<SystemTypeNormalActivity>(
                    Bundle().apply {
                        putSerializable("info", systemAdapter.getItem(position)!!)
                    })
            }
        }
        helper = QuickAdapterHelper.Builder(systemAdapter).build()

        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = helper.adapter

        }

        binding.refreshLayout.apply {
            setOnRefreshListener {
                viewModel.sendIntent(SystemViewModel.SystemUiIntent.GetSystemTypes(true))
            }
        }
    }

    override fun initData() {
        viewModel.sendIntent(SystemViewModel.SystemUiIntent.GetSystemTypes(true))
    }

    override fun observeStateAndEvent() {
        viewModel.uiStateFlow.collectFLow(this){
            when (this) {
                is SystemViewModel.SystemUiState.Init -> {
                }

                is SystemViewModel.SystemUiState.IsRefresh -> {
                    binding.refreshLayout.isRefreshing = this.isRefresh
                }

                is SystemViewModel.SystemUiState.FetchSystemParents -> {
                    this.systemParents?.let {
                        systemAdapter.submitList(it)
                        // 如果在数据不满足一屏时，暂停加载更多，请调用下面方法
                        helper.trailingLoadStateAdapter?.checkDisableLoadMoreIfNotFullPage()
                        helper.trailingLoadState = LoadState.NotLoading(true)

                    }
                }
            }
        }

        viewModel.effectFlow.collectFLow(this) {
            when (this) {
                is LoginViewModel.LoginEvent.ToastEvent -> {
                    requireActivity().toast(
                        this.message, if (this.short) Toast.LENGTH_SHORT else Toast.LENGTH_LONG
                    )
                }
            }
        }
    }
}